//
//  HTNetworkTools.swift
//  Swift网络请求管理类
//
//  Created by 郝涛 on 16/4/19.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
import AFNetworking
//  请求方式
enum HTRequestWay:Int {
    
    case GET = 0
    case POST = 1
}

class HTNetworkTools: AFHTTPSessionManager {
    
        static let sharedNetworkTools :HTNetworkTools = {
            
          let tools = HTNetworkTools ()
            
          tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
            
            return tools
       
    }()
    
    //请求
    func request(Way:HTRequestWay ,urlString:String ,param:AnyObject? ,callBlck:(response:AnyObject? ,error:NSError?)->()){
        
        if Way == .GET{
            
            self.GET(urlString, parameters: param, progress: nil, success: { (_,response) -> Void in
                //成功
                callBlck(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                //失败
                callBlck(response: nil, error: error)
            })
        }else{
            self.POST(urlString, parameters: param, progress: nil, success: { (_,response) -> Void in
                //成功
                callBlck(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    //失败
                    
                    callBlck(response: nil, error: error)
            })
            
            
        }
        
    }
    
}
extension HTNetworkTools{
    
    func loadStatus(accessToken:String, since_id:Int64,max_id:Int64,callback:(response:AnyObject? ,error:NSError?)->()){
        //请求url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数
        let param = [
            "access_token":accessToken,
            "since_id":"\(since_id)",
            "max_id":"\(max_id)"
        ]
        
        request(HTRequestWay.GET, urlString: urlString, param: param) { (response, error) -> () in
            callback(response: response, error: error)
        }
  
    }

}
























